import 'package:flutter/material.dart';

import '../app/config.dart';

class FixedTabIndicator extends Decoration {
  const FixedTabIndicator({
     this.color,
     this.width = 20,
     this.height = 4,
    this.offset = 0.0,
  });

  final Color? color;
  final double width;
  final double height;
  final double offset;

  @override
  BoxPainter createBoxPainter([VoidCallback? onChanged]) {
    return _FixedTabIndicatorPainter(this, onChanged);
  }
}

class _FixedTabIndicatorPainter extends BoxPainter {
  _FixedTabIndicatorPainter(this.decoration, VoidCallback? onChanged)
      : super(onChanged);

  final FixedTabIndicator decoration;

  @override
  void paint(Canvas canvas, Offset offset, ImageConfiguration configuration) {
    assert(configuration.size != null);
    final Rect rect = offset & configuration.size!;
    final Paint paint = Paint()
      ..color = decoration.color?? appGlobalConfig.widgetConfig.indicatorColor
      ..strokeWidth = decoration.height
      ..strokeCap = StrokeCap.round;

    // 计算线的起始和结束位置，使其在选项卡的中心
    final double centerX = rect.left + rect.width / 2;
    final double startX = centerX - decoration.width / 2;
    final double endX = centerX + decoration.width / 2;
    final double dy = rect.bottom - decoration.offset;

    canvas.drawLine(
      Offset(startX, dy),
      Offset(endX, dy),
      paint,
    );
  }
}